package com.yz.controller;

import com.yz.core.exception.JsonException;
import com.yz.core.utils.R;
import com.yz.entity.UserEntity;
import com.yz.service.impl.InvitataionScoreService;
import com.yz.service.impl.LikeService;
import com.yz.vo.LikeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @className: LikeController
 * @author: shuishui
 * @date: 2023/01/04 20:38
 */
@RestController
@RequestMapping("/like")
public class LikeController {
    @Autowired
    private LikeService likeService;
    @Autowired
    private InvitataionScoreService scoreService;
    @PostMapping("/clickLike")
    public R clickLike(@RequestBody LikeVo likeVo, HttpSession session){
        UserEntity userEntity = (UserEntity) session.getAttribute("loginUser");
        if (userEntity==null){
            throw new JsonException(301,"未登录");
        }
        likeVo.setUserId(userEntity.getId());//谁点的赞，把点赞人的id设置进来
        likeService.clickLike(likeVo);//实现点赞操作
        if (likeVo.getEntityType()==1){
            scoreService.addIdToRedis(likeVo.getEntityId());
        }
        Boolean isLike = likeService.isLike(likeVo);
        long likeCount = likeService.getInvitataionLikeCount(likeVo.getEntityType(),likeVo.getEntityId());
        //这里的r为map，重写了put方法，通过put和三元表达式赋值
        R ok = R.ok();
        ok.put("likeStatus",isLike?1:0);
        ok.put("likeCount", likeCount);
        return ok;
    }
}
